package com.project.restaurant.meal.service.impl;


import com.project.restaurant.common.api.ServiceReturn;
import com.project.restaurant.common.api.ServiceStateCode;
import com.project.restaurant.mapper.MealTypeLevelMapper;
import com.project.restaurant.mapper.MealTypeMapper;
import com.project.restaurant.meal.dao.MealDetailDao;
import com.project.restaurant.meal.dto.MealDetail;
import com.project.restaurant.meal.service.MealMenuService;
import com.project.restaurant.meal.vo.MealMenuVo;
import com.project.restaurant.pojo.MealType;
import com.project.restaurant.pojo.MealTypeLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MealMenuServiceImpl implements MealMenuService {

    @Autowired
    MealDetailDao mealDao;

    @Autowired
    MealTypeLevelMapper levelMapper;

    @Autowired
    MealTypeMapper typeMapper;

    @Override
    public ServiceReturn<List<MealMenuVo>> generateMenu() {
        List<MealDetail> data = mealDao.selectAll();
        Map<Integer,MealType> mealTypeMap = typeMapper.selectAll();
        List<MealTypeLevel> levelList = levelMapper.selectAll();
        return new ServiceReturn<>(ServiceStateCode.SUCCESS,createMenu(levelList,mealTypeMap,data));
    }

    private List<MealMenuVo> createMenu(List<MealTypeLevel> levelList,Map<Integer,MealType> mealTypeMap,
                                        List<MealDetail> data){
        HashMap<Integer,List<MealDetail>> dataMap = new HashMap<>();
        data.forEach(meal->{
            List<MealDetail> mealList = dataMap.getOrDefault(meal.getMealTypeId(), new ArrayList<>());
            mealList.add(meal);
            dataMap.put(meal.getMealTypeId(),mealList);
        });

        List<MealMenuVo> menus = new ArrayList<>();
        HashMap<Integer, MealMenuVo> menuMap = new HashMap<>();
        for(MealTypeLevel level:levelList){
            Integer parentId = level.getMealTypeParentId();
            MealMenuVo parent = menuMap.get(parentId);
            if(parentId != -1 && parent == null){
                parent = new MealMenuVo(mealTypeMap.get(parentId));
                List<MealDetail> mealData = dataMap.get(parentId);
                if(mealData!=null){
                    parent.addMealList(mealData);
                }
                menuMap.put(parentId,parent);
            }
            Integer childId = level.getMealTypeId();
            MealMenuVo child = menuMap.get(childId);
            if(childId != -1 && child == null){
                child = new MealMenuVo(mealTypeMap.get(childId));
                List<MealDetail> mealData = dataMap.get(childId);
                if(mealData!=null){
                    child.addMealList(mealData);
                }
                menuMap.put(childId,child);
            }
            if(parentId == -1){
                menus.add(child);
                continue;
            }
            parent.addChildMenu(child);

        }
        return menus;
    }

}
